package com.briup.cms.service;

import com.briup.cms.bean.BaseLog;
import com.briup.cms.bean.BaseUser;
import com.briup.cms.dao.UserMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.exception.LoginException;
import com.briup.cms.utils.JwtUtil;
import com.briup.cms.utils.MessageStatus;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class BaseUserServiceImpl implements IBaseUserService{

    @Autowired
    private UserMapper mapper;

    public BaseUserServiceImpl() {
    }

    @Override
    public String login(String username, String password) throws LoginException {

        BaseUser user = new BaseUser();
//        int errorTimes = 0;
        String token= null;
        user.setUsername(username);
        user.setPassword(password);

        // 把用户名和密码放到 dao层去查询
        BaseUser ck_username = mapper.findUserByUsername(user.getUsername());
        if(ck_username==null){
            // 用户名不存在
            //System.out.println("用户名不存在！"+new LoginException(MessageStatus.USER_NOT_EXIST));
            throw new LoginException(MessageStatus.USER_NOT_EXIST);

        }
        // 密码比对是否一致
        // System.out.println(password+"..."+ck_username.getPassword());
        if(!password.equals(ck_username.getPassword())){
            //LoginException errorPwd = new LoginException(MessageStatus.USER_LOGIN_ERROR);
            //System.out.println("密码错误！"+new LoginException(MessageStatus.USER_LOGIN_ERROR));
            throw new LoginException(MessageStatus.USER_LOGIN_ERROR);
        }
        // 账号被禁用
        if(ck_username.getStatus() == 1){
            throw new LoginException(MessageStatus.USER_ACCOUNT_FORBIDDEN);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("username",ck_username.getUsername());
        map.put("realname",ck_username.getRealname());


        token = JwtUtil.sign(ck_username.getUsername(), map);
        // System.out.println("操作成功！");
        return token;
    }

    /**
     *  新增的用户管理模块
     *
     */

    @Override
    public BaseUser currentUserInfo(String token) throws CustomerException {
        if(token == null){
            throw new LoginException(MessageStatus.USER_TOKEN_NOT_EXIST);
        }
        try {
            JwtUtil.checkSign(token);
        } catch (Exception e) {
            throw new LoginException(MessageStatus.USER_TOKEN_INVALID);
        }
        String username = JwtUtil.getUserId(token);
        return mapper.findUserByUsername(username);
    }

    @Override
    public PageInfo<BaseUser> pageQuery(int pageNum, int pageSize, String username, Integer role) {
        PageHelper.startPage(pageNum, pageSize,true);
        List<BaseUser> list = mapper.findUserByUsernameAndRoleLike(username,role);
        PageInfo<BaseUser> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public void updateUser(BaseUser user) throws CustomerException {
        mapper.updateUser(user);
    }

    @Override
    public void updateUserIcon(Integer id, String alterUserIcon) throws CustomerException {
        mapper.updateUserIcon(id,alterUserIcon);
    }
}
